"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

import threading
from msnp.service.utils import DateFormatter

__shouldLogToFile = False
__testResult = "testOutput.txt"
__filename = "logs/"

#this lock is needed to guarantee that only one thread accesses the file
__writeLock = threading.Lock()

def setLogToFile(log):
    """This methos sets the global boolean parameter __shouldLogToFile to the given one."""
    global __shouldLogToFile
    __shouldLogToFile = log

def initTestResultFile():
    """This method cleans the test-result file named by the global parameter __testResult."""
    global __testResult
    
    __cleanFile(__testResult)
    
def initLogFile(fname):
    """This method cleans the log file indicated by the given parameter and sets the global parameter __filename."""
    global __filename
    
    __filename += ("logs" + fname + ".txt")
    __cleanFile(__filename)

def info(s):
    """This method logs the given string."""
    global __writeLock
    
    __writeLock.acquire()
    try:
        __logToShell(s)
        __logToFile(s)
    finally:
        __writeLock.release()
        
def ptpDBinfo(s):
    """This method logs the given string. Is called only by methods related to the ptp database."""
    global __writeLock
    
    __writeLock.acquire()
    try:
        __logToShell(s)
        __logToFile(s)
    finally:
        __writeLock.release()
        
def mediaDBinfo(s):
    """This method logs the given string. Is called only by methods related to the media database."""
    global __writeLock
    
    __writeLock.acquire()
    try:
        __logToShell(s)
        __logToFile(s)
    finally:
        __writeLock.release()

def guiInfo(s):
    """This method logs the given string. Is called only by methods related to the gui."""
    global __writeLock
    
    __writeLock.acquire()
    try:
        __logToShell(s)
        __logToFile(s)
    finally:
        __writeLock.release()
    
def logResult(s):
    """This method is used to log test results about the whole test run in a seperate file given by the global parameter __testResult."""
    global __testResult
    
    __appendToFile(s + "\n", __testResult)

def __cleanFile(fname):
    """This method cleans a specific file"""
#    f = open(fname, 'w')
#    f.write("")
#    f.close()
    pass
    
def __appendToFile(s, fName):
    """This method appends a given string s to the file fname"""
#    f = open(fName, 'a')
#    f.write(s)
#    f.close()
    pass

def __getFormattedString(s):
    """This method appends the current time in front of the given string."""
    dateString = DateFormatter.formatTimeWithoutDay()
    logString = dateString + " - " + s
    return logString
    
def __logToShell(s):
    """This method writes the given string to the standard output."""
    s = __getFormattedString(s)
    print s

def __logToFile(s):
    """This method writes the given string to the file named by the global parameter __filename."""
    global __filename
    global __shouldLogToFile
    
    if __shouldLogToFile:
        s = __getFormattedString(s) + "\n"
        __appendToFile(s, __filename)
